package com.api.volunteer.controller;

import com.api.volunteer.dao.MessageRecordDao;
import com.api.volunteer.model.MessageRecord;
import com.api.volunteer.model.UserEntity;
import com.api.volunteer.model.VolunteerActivities;
import com.api.volunteer.repository.UserRepository;
import com.api.volunteer.repository.VolunteerActivitesRepository;
import com.api.volunteer.ro.MessageRO;
import com.api.volunteer.service.VolunteerActivitesService;
import com.api.volunteer.util.Holder;
import com.api.volunteer.util.Result;
import com.api.volunteer.vo.VolunteerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 邹宇杰
 * @email 2901570623@qq.com
 * @date 2022/4/1 12:01
 */
@RestController
public class VolunteerActivitesController extends BaseController{

    @Autowired
    SimpleDateFormat dateFormat;

    @Autowired
    VolunteerActivitesService volunteerActivitesService;

    @Autowired
    MessageRecordDao messageRecordDao;

    @Autowired
    UserRepository userRepository;

    @Autowired
    Holder holder;
    /**
     * 发布志愿活动
     * @param volunteerVo 前端传输得志愿活动信息vo
     * @return 发布响应状态
     */
    @PostMapping("/pushVolunteer")
    public Result pushVolunteer(@RequestBody VolunteerVo volunteerVo){

        VolunteerActivities volunteerActivities = new VolunteerActivities();

        BeanUtils.copyProperties(volunteerVo, volunteerActivities);

        volunteerActivitesService.save(volunteerActivities);

        return this.processSuceess();
    }

    /**
     * 加入志愿活动
     * @param volunteerId 志愿活动id
     * @return 加入响应状态
     */
    @PostMapping("/joinVolunteer")
    public Result joinVolunteer(@RequestParam("volunteerId") String volunteerId){
        volunteerActivitesService.joinVolunteer(holder.getUser().getOpenid(), volunteerId);
        return this.processSuceess();
    }

    @GetMapping("/listVolunteers")
    public Result list(){
        List<VolunteerActivities> list = volunteerActivitesService.list();
        return this.processData(()-> list);
    }

    @GetMapping("/checkVolunteer")
    public Result checkVolunteer(@RequestParam("volunteerId") String volunteerId){
        VolunteerActivities volunteerActivities = volunteerActivitesService.getVolunteerActivities(volunteerId);
        return this.processData(()-> volunteerActivities);
    }

    @GetMapping("/listMessageRecord")
    public Result list(@RequestParam("volunteerId") String volunteerId, @RequestParam("openid") String openid, @RequestParam("otherOpenid") String otherOpenid){

        UserEntity openidEntity = userRepository.findUserEntityByOpenid(openid);

        UserEntity toOpenidEntity = userRepository.findUserEntityByOpenid(otherOpenid);

        List<MessageRO> collect = volunteerActivitesService.listMessage(volunteerId, openid, otherOpenid).stream().map((messageRecord) -> {
            MessageRO messageRO = new MessageRO();
            BeanUtils.copyProperties(messageRecord, messageRO);
            messageRO.setOpenidNickName(openidEntity.getNickName());
            messageRO.setToOpenidNickName(toOpenidEntity.getNickName());
            messageRO.setOpenidUrl(openidEntity.getAvatarUrl());
            messageRO.setToOpenidUrl(toOpenidEntity.getAvatarUrl());
            return messageRO;
        }).collect(Collectors.toList());

        return this.processData(()->collect);
    }

    @GetMapping("/listSession")
    public Result listSession(){
        List<MessageRecord> historyRecord = messageRecordDao.findHistoryRecord(holder.getUser().getOpenid());
        return this.processData(()->historyRecord);
    }
}
